import os
from transformers import pipeline
from transformers import AutoTokenizer
from transformers import AutoModelForSequenceClassification

# 本地模型文件目录
model_dir = os.path.join('D:', os.path.sep, 'ModelSpace', 'Qwen2.5', 'Qwen2.5-1.5B-Instruct')

# 加载分词器
tokenizer = AutoTokenizer.from_pretrained(
    model_dir,
    local_files_only=True,
)

# 加载本地模型
model = AutoModelForSequenceClassification.from_pretrained(
    model_dir,
    torch_dtype="auto",
    device_map="auto",
    local_files_only=True,
)

# 创建一个情感分析Pipeline
nlp = pipeline("text-classification", tokenizer=tokenizer, model=model)

# 对一段文本进行情感分析
result = nlp("我觉得使用Transformers库Pipeline处理NLP任务非常方便！")

# 输出：[{'label': 'LABEL_0', 'score': 0.9700134992599487}]
print(result)
